import Vuex from "vuex";
import Vue from "vue";
import tabBar from "./tabBar";
import {
    getSessionStorageObj,
    setSessionStorageObj,
    getDeviceInfo,
    isPC
} from "../js/utils";

const modulesFile = require.context("./modules", false, /\.js$/);
let modules = {};
modulesFile.keys().forEach(key => {
    let name = key.replace(/(.*\/)*([^.]+).*/gi, "$2");
    modules[name] = modulesFile(key).default;
});

Vue.use(Vuex);
export const store = new Vuex.Store({
    state: {
        userId: getSessionStorageObj("userId"),
        loading: false,
        device: getDeviceInfo(), //设备信息
        userInfo: getSessionStorageObj("userInfo"), //微信userinfo
        inited: getSessionStorageObj("inited"),
        appData: {}, //jsApiTicket
        isPC: isPC()
    },
    mutations: {
        setAppData(state, payload) {
            state.appData = payload;
            // setSessionStorageObj("appData", payload);
        },
        setUserId(store, userId) {
            store.userId = userId;
            setSessionStorageObj("userId", userId);
        },
        setUserInfo(store, userInfo) {
            store.userInfo = userInfo;
            store.userId = userInfo.openId;
            setSessionStorageObj("userId", userInfo.openId);
            setSessionStorageObj("userInfo", userInfo);
        },
        showLoading(state) {
            state.loading = true;
        },
        hideLoading(state) {
            state.loading = false;
        },
        setInit(state) {
            state.inited = true;
            setSessionStorageObj("inited", true);
        }
    },
    actions: {},
    modules: {
        tabBar,
        ...modules
    }
});
